package com.xbongbong.paas.help.menu;

import com.alibaba.fastjson.JSONObject;
import com.xbongbong.paas.pojo.SummaryDataPoJo;
import com.xbongbong.paas.service.init.InitMenuService;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.helper.ReflectHelper;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import com.xbongbong.pro.menu.pojo.dto.MenuInitDTO;
import com.xbongbong.pro.menu.pojo.vo.MenuInitVO;
import com.xbongbong.saas.enums.CompanyConfigEnum;
import com.xbongbong.saas.enums.XbbRefTypeEnum;
import com.xbongbong.saas.enums.business.AssembleEnum;
import com.xbongbong.saas.enums.business.BomBillEnum;
import com.xbongbong.saas.enums.business.CommunicatePlanEnum;
import com.xbongbong.saas.enums.business.CompetitorAnalysisEnum;
import com.xbongbong.saas.enums.business.CompetitorEnum;
import com.xbongbong.saas.enums.business.ContactEnum;
import com.xbongbong.saas.enums.business.ContractEnum;
import com.xbongbong.saas.enums.business.CustomerManagementEnum;
import com.xbongbong.saas.enums.business.DistributorEnum;
import com.xbongbong.saas.enums.business.InstockEnum;
import com.xbongbong.saas.enums.business.InventoryEnum;
import com.xbongbong.saas.enums.business.InvoiceEnum;
import com.xbongbong.saas.enums.business.OutstockEnum;
import com.xbongbong.saas.enums.business.PayPlanEnum;
import com.xbongbong.saas.enums.business.PaymentEnum;
import com.xbongbong.saas.enums.business.ProductEnum;
import com.xbongbong.saas.enums.business.ProductionOrderEnum;
import com.xbongbong.saas.enums.business.PurchaseEnum;
import com.xbongbong.saas.enums.business.PurchaseInvoiceEnum;
import com.xbongbong.saas.enums.business.QuotationEnum;
import com.xbongbong.saas.enums.business.RefundEnum;
import com.xbongbong.saas.enums.business.ReturnedPurchaseEnum;
import com.xbongbong.saas.enums.business.SalesOpportunityEnum;
import com.xbongbong.saas.enums.business.SupplierContactEnum;
import com.xbongbong.saas.enums.business.SupplierEnum;
import com.xbongbong.saas.enums.business.TransferEnum;
import com.xbongbong.saas.enums.business.WarehouseEnum;
import com.xbongbong.sys.domain.entity.CompanyConfigEntity;
import com.xbongbong.sys.model.CompanyConfigModel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Random;

/**
 * 菜单初始化
 * @author 吴峰
 * @version v1.0
 * @date 2019/1/15 19:51
 * @desrcption 菜单初始化
 * @since v1.0
 */
@Component
public class InitMenuHelp {

    private static final Logger LOG = LoggerFactory.getLogger(InitMenuHelp.class);


    @Resource
    private InitCrmMenuHelp initCrmMenuHelp;

    @Resource
    private CompanyConfigModel companyConfigModel;
    @Resource
    private Map<String, InitMenuService> initMenuMap;


    @Transactional(rollbackFor = Exception.class)
    public MenuInitVO menuInit(MenuInitDTO menuInitDTO) throws XbbException {
        try {
            // 创建应用
            InitMenuService initMenuService = initMenuMap.get(menuInitDTO.getAlias());
            return initMenuService.open(menuInitDTO);
        } catch (XbbException e){
            LOG.error("MenuInitHelp.menuInit初始化菜单出错", e);
            throw e;
        } catch (Exception e){
            LOG.error("MenuInitHelp.menuInit初始化菜单出错", e);
            throw new XbbException(SystemErrorCodeEnum.API_ERROR_100001, SystemErrorCodeEnum.API_ERROR_100001.getMsg());
        }

    }












    /**
     * CRM 简化版 - 初始化应用、菜单、表单、表单解释
     *
     * @param menuInitDTO
     * @return
     * @author xingxing.xiao
     * @date 2019/11/26 14:17
     */
    @Transactional(rollbackFor = Exception.class)
    public void simpleCrmInit(MenuInitDTO menuInitDTO) throws XbbException {
        initCrmMenuHelp.simpleCrmInit(menuInitDTO);
    }



    /**
     * 获取表单默认label 移动端详情页展示
     * @param businessType 业务类型
     */
    public List<SummaryDataPoJo> getAppDefaultLabelSimple(Integer businessType) {
        List<Object> labelAttrList;
        XbbRefTypeEnum xbbRefTypeEnum = XbbRefTypeEnum.getByCode(businessType);
        switch (xbbRefTypeEnum) {
            case CUSTOMER_MANAGEMENT:
            case CUSTOMER:
            case CUSTOMER_FOCUS:
            case CUSTOMER_DEAL:
            case CUSTOMER_FOLLOW:
            case CUSTOMER_PUBLIC:
            case CUSTOMER_RECYCLE:
            case CUSTOMER_NEARBY:
                labelAttrList = CustomerManagementEnum.getLabelAttrListSimple();
                break;
            case CONTACT:
                labelAttrList = ContactEnum.getLabelAttrList();
                break;
            case CONTRACT:
                labelAttrList = ContractEnum.getLabelAttrList();
                break;
            case SUPPLIER_CONTACT:
                labelAttrList = SupplierContactEnum.getLabelAttrList();
                break;
            case PURCHASE:
                labelAttrList = PurchaseEnum.getLabelAttrList();
                break;
            case QUOTATION:
                labelAttrList = QuotationEnum.getLabelAttrList();
                break;
            case DISTRIBUTOR_MANAGEMENT:
                labelAttrList = DistributorEnum.getLabelAttrListSimple();
            default:
                labelAttrList = new ArrayList<>();
                break;
        }
        List<SummaryDataPoJo> summaryDataPoJoList = new ArrayList<>();
        for (Object object : labelAttrList) {
            if (Objects.nonNull(object)) {
                Object attr;
                Object attrName;
                Object fieldType;
                if (object instanceof JSONObject) {
                    JSONObject itemObj = (JSONObject) object;
                    attr = itemObj.get("attr");
                    attrName = itemObj.get("attrName");
                    fieldType = itemObj.get("fieldType");
                } else {
                    attr = ReflectHelper.valueGet(object, "attr");
                    attrName = ReflectHelper.valueGet(object, "attrName");
                    fieldType = ReflectHelper.valueGet(object, "fieldType");
                }
                SummaryDataPoJo summaryDataPoJo = new SummaryDataPoJo();
                if (Objects.nonNull(attr)) {
                    ReflectHelper.valueSet(summaryDataPoJo, "attr", attr);
                }
                if (Objects.nonNull(attrName)) {
                    ReflectHelper.valueSet(summaryDataPoJo, "attrName", attrName);
                }
                if (Objects.nonNull(fieldType)) {
                    ReflectHelper.valueSet(summaryDataPoJo, "fieldType", fieldType);
                }
                summaryDataPoJoList.add(summaryDataPoJo);
            }
        }
        return summaryDataPoJoList;
    }


    /**
     * 获取表单默认summary 移动端详情页展示
     * @param businessType 业务类型
     * @return List
     */
    public List<SummaryDataPoJo> getAppDefaultSummarySimple(Integer businessType) {
        List<Object> summaryList;
        XbbRefTypeEnum xbbRefTypeEnum = XbbRefTypeEnum.getByCode(businessType);
        switch (xbbRefTypeEnum) {
            case CUSTOMER_MANAGEMENT:
            case CUSTOMER:
            case CUSTOMER_FOCUS:
            case CUSTOMER_DEAL:
            case CUSTOMER_FOLLOW:
            case CUSTOMER_PUBLIC:
            case CUSTOMER_RECYCLE:
            case CUSTOMER_NEARBY:
                summaryList = CustomerManagementEnum.getSummaryAttrSimpleList();
                break;
            case CONTACT:
                summaryList = ContactEnum.getSummaryAttrList();
                break;
            case SALES_OPPORTUNITY:
            case ALL_OPPORTUNITY:
            case KEY_OPPORTUNITY:
                summaryList = SalesOpportunityEnum.getSummaryAttrList();
                break;
            case CONTRACT:
                summaryList = ContractEnum.getSummaryAttrList();
                break;
            case REFUND:
                summaryList = RefundEnum.getSummaryAttrList();
                break;
            case PAYMENT:
                summaryList = PaymentEnum.getSummaryAttrList();
                break;
            case PAY_PLAN:
                summaryList = PayPlanEnum.getSummaryAttrList();
                break;
            case INVOICE:
                summaryList = InvoiceEnum.getSummaryAttrList();
                break;
            case PURCHASE_INVOICE:
                summaryList = PurchaseInvoiceEnum.getSummaryAttrList();
                break;
            case SUPPLIER:
                summaryList = SupplierEnum.getSummaryAttrList();
                break;
            case SUPPLIER_CONTACT:
                summaryList = SupplierContactEnum.getSummaryAttrList();
                break;
            case PURCHASE:
                summaryList = PurchaseEnum.getSummaryAttrList();
                break;
            case RETURNED_PURCHASE:
                summaryList = ReturnedPurchaseEnum.getSummaryAttrList();
                break;
            case INSTOCK:
            case PURCHASE_INSTOCK:
            case REFUND_INSTOCK:
            case OTHER_INSTOCK:
            case PRODUCTION_INSTOCK:
            case RETURNED_MATERIEL_INSTOCK:
                summaryList = InstockEnum.getSummaryAttrList();
                break;
            case OUTSTOCK:
            case CONTRACT_OUTSTOCK:
            case RETURNED_PURCHASE_OUTSTOCK:
            case OTHER_OUTSTOCK:
            case PRODUCTION_MATERIEL_OUTSTOCK:
            case WORK_ORDER_OUTSTOCK:
                summaryList = OutstockEnum.getSummaryAttrList();
                break;
            case INVENTORY:
                summaryList = InventoryEnum.getSummaryAttrList();
                break;
            case TRANSFER:
                summaryList = TransferEnum.getSummaryAttrList();
                break;
            case ASSEMBLE:
                summaryList = AssembleEnum.getSummaryAttrList();
                break;
            case WAREHOUSE:
                summaryList = WarehouseEnum.getSummaryAttrList();
                break;
            case BOM_BILL:
                summaryList = BomBillEnum.getSummaryAttrList();
                break;
            case PRODUCTION_ORDER:
                summaryList = ProductionOrderEnum.getSummaryAttrList();
                break;
            case COMMUNICATE_PLAN:
                summaryList = CommunicatePlanEnum.getSummaryAttrList();
                break;
            case PRODUCT:
                summaryList = ProductEnum.getSummaryAttrList();
                break;
            case QUOTATION:
                summaryList = QuotationEnum.getSummaryAttrList();
                break;
            case DISTRIBUTOR_MANAGEMENT:
                summaryList = DistributorEnum.getSummaryAttrSimpleList();
                break;
            default:
                summaryList = new ArrayList<>();
                break;
        }
        List<SummaryDataPoJo> summaryDataPoJoList = new ArrayList<>();
        for (Object object : summaryList) {
            if (Objects.nonNull(object)) {
                Object attr;
                Object attrName;
                Object fieldType;
                if (object instanceof JSONObject) {
                    JSONObject itemObj = (JSONObject) object;
                    attr = itemObj.get("attr");
                    attrName = itemObj.get("attrName");
                    fieldType = itemObj.get("fieldType");
                } else {
                    attr = ReflectHelper.valueGet(object, "attr");
                    attrName = ReflectHelper.valueGet(object, "attrName");
                    fieldType = ReflectHelper.valueGet(object, "fieldType");
                }
                SummaryDataPoJo summaryDataPoJo = new SummaryDataPoJo();
                if (Objects.nonNull(attr)) {
                    ReflectHelper.valueSet(summaryDataPoJo, "attr", attr);
                }
                if (Objects.nonNull(attrName)) {
                    ReflectHelper.valueSet(summaryDataPoJo, "attrName", attrName);
                }
                if (Objects.nonNull(fieldType)) {
                    ReflectHelper.valueSet(summaryDataPoJo, "fieldType", fieldType);
                }
                summaryDataPoJoList.add(summaryDataPoJo);
            }
        }
        return summaryDataPoJoList;
    }




    private static Random random = new Random();
    /**
     * 是否使用镜像
     * @author xingxing.xiao
     * @date 2019/12/11 11:05
     */
    public boolean isUseMirror() {
        CompanyConfigEntity companyConfigEntity = companyConfigModel.getByConfigAlias(CompanyConfigEnum.MIRROR_PERCENT.getAlias(), "0");
        if (Objects.nonNull(companyConfigEntity)) {
            String configValue = companyConfigEntity.getConfigValue();
            Double percent = Double.valueOf(configValue);
            int ran = random.nextInt(100);
            return ran < percent * 100;
        }
        return false;
    }








}
